
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class FamilyModify extends JFrame{
    public FamilyModify(String userid, Object familyID){

        SQLOperation sql=new SQLOperation();
        setTitle("Family Modify");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel("Modify Information of "+familyID);
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);

        add(titlePanel,BorderLayout.NORTH);

        String[] familyAttributes={"familyID","fName","address"};
        Object[][] detailInfo=sql.search("Family",familyAttributes,"familyID",familyID);



        JPanel select=new JPanel();

        JComboBox attributeSelect=new JComboBox();
        attributeSelect.addItem("----------");
        attributeSelect.addItem("fName");
        attributeSelect.addItem("address");

        JTextField enter=new JTextField(20);

        select.add(attributeSelect);
        select.add(enter);

        add(select,BorderLayout.CENTER);

        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FamilySearchResult(userid,familyID);
            }
        });

        JButton modify=new JButton("Modify it");
        modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(attributeSelect.getSelectedIndex()==0||enter.getText().equals("")){
                    JOptionPane.showMessageDialog
                            (null, "Fail Modify Update!","Wrong Input",JOptionPane.ERROR_MESSAGE);

                }
                else{
                    sql.modify("Family","familyID",familyID,
                            (String)attributeSelect.getSelectedItem(),enter.getText());
                    JOptionPane.showMessageDialog
                            (null, "Modify Complete!","Success",JOptionPane.INFORMATION_MESSAGE);
                   setVisible(false);
                    new FamilySearchResult(userid,familyID);

                }


            }
        });



        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(modify);

        add(buttons,BorderLayout.SOUTH);



        setSize(400,150);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);

    }
}
